package com.ctg.itrdc.sysmgr.portal.control;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.api.dto.PlatInfoDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IPlatInfoService;
import com.ctg.itrdc.sysmgr.portal.api.service.IStaffService;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService;
import com.ctg.itrdc.sysmgr.portal.common.constant.DictEnum;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	ISystemUserService userService;
	
	@Resource
	IStaffService staffService;
	
	@Resource
	IPlatInfoService platInfoService;
	
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	public CommonResult insert(@RequestBody SystemUserDTO dto) {
		if(dto.getSysUserCode() == null || dto.getPassword() == null) {
			return new CommonResult(HttpStatus.UNPROCESSABLE_ENTITY.toString(), "用户名/密码不能为空，请检查");
		}
		Long staffId = dto.getStaffId();
		StaffDTO staff = staffService.selectByPrimaryKey(staffId);
		if(staff.getStaffCode() == null) {
			return new CommonResult(HttpStatus.UNPROCESSABLE_ENTITY.toString(), "员工信息错误，请检查");
		}
		if(dto.getSysCode() == null) {
			return new CommonResult(HttpStatus.UNPROCESSABLE_ENTITY.toString(),"平台编码错误，请检查");
		}
		PlatInfoDTO info = platInfoService.selectByPlatSysNbr(dto.getSysCode());
		if(info == null) {
			return new CommonResult(HttpStatus.UNPROCESSABLE_ENTITY.toString(),"平台编码错误，请检查");
		}
		SystemUserDTO tempDTO = userService.findUserByLoginName(dto.getSysUserCode());
		if(tempDTO != null) {
			return new CommonResult(HttpStatus.NOT_ACCEPTABLE.toString(), "用户已存在");
		}
		dto.setStaffName(staff.getStaffName());
		dto.setStatusCd(DictEnum.StatusCd.S0A.toString());
		dto.setPwdNewtime(new Timestamp(System.currentTimeMillis()));
		dto.setCreateDate(new Timestamp(System.currentTimeMillis()));
		dto.setPwdErrCnt(3);
		dto.setCreateStaff(staff.getStaffId());
		dto.setUpdateStaff(staff.getStaffId());
		Long id = userService.insert(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功",id);
	}
	
	@RequestMapping(method=RequestMethod.GET)
	@ResponseBody
	public CommonResult getUser(Long sysUserId) {
		SystemUserDTO user = userService.selectByPrimaryKey(sysUserId);
		if(user.getSysUserCode() == null) {
			return new CommonResult(HttpStatus.NO_CONTENT.toString(), "用户不存在");
		}
		return new CommonResult(HttpStatus.OK.toString(), "查询成功", user);
	}
	
	@RequestMapping(value="/byCode",method=RequestMethod.GET)
	@ResponseBody
	public CommonResult getUserByCode(String sysUserCode) {
		SystemUserDTO user = userService.findUserByLoginName(sysUserCode);
		if(user == null) {
			return new CommonResult(HttpStatus.NO_CONTENT.toString(), "用户不存在");
		}
		return new CommonResult(HttpStatus.OK.toString(), "查询成功", user);
	}
	
	@RequestMapping(method=RequestMethod.PUT)
	@ResponseBody
	public CommonResult updateUser(@RequestBody SystemUserDTO dto) {
		if(dto.getStaffId() == null) return new CommonResult(HttpStatus.OK.toString(), "员工id为空，操作终止");
		if(dto.getSysCode() == null) return new CommonResult(HttpStatus.OK.toString(), "平台编码为空，操作终止");
		
		SystemUserDTO user = userService.selectByPrimaryKey(dto.getSysUserId());
		if(user.getSysUserCode() == null) {
			return new CommonResult(HttpStatus.NO_CONTENT.toString(), "用户不存在");
		}
		if(!dto.getSysCode().equals(user.getSysCode())) {
			return new CommonResult(HttpStatus.BAD_REQUEST.toString(), "用户不属于该平台，无法修改");
		}
		if(!dto.getStaffId().equals(user.getStaffId())) {
			return new CommonResult(HttpStatus.BAD_REQUEST.toString(), "用户不属于该员工，无法修改");
		}
		userService.updateSelectiveByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "修改成功");
	}
	
	@RequestMapping(method=RequestMethod.DELETE)
	@ResponseBody
	public CommonResult deleteUser(SystemUserDTO dto) {
		if(dto.getStaffId() == null) return new CommonResult(HttpStatus.OK.toString(), "员工id为空，操作终止");
		if(dto.getSysCode() == null) return new CommonResult(HttpStatus.OK.toString(), "平台编码为空，操作终止");
		SystemUserDTO user = userService.selectByPrimaryKey(dto.getSysUserId());
		if(user.getSysUserCode() == null) {
			return new CommonResult(HttpStatus.NO_CONTENT.toString(), "用户不存在");
		}
		if(!user.getStaffId().equals(dto.getStaffId())) {
			return new CommonResult(HttpStatus.BAD_REQUEST.toString(), "用户不属于该员工，无法删除");
		}
		if(!user.getSysCode().equals(dto.getSysCode())) {
			return new CommonResult(HttpStatus.BAD_REQUEST.toString(), "用户不属于该平台，无法删除");
		}
		
		userService.deleteByPrimaryKey(dto.getSysUserId());

		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}
	
}
